package cs342.project1.mathNotebook.gui;

import java.awt.Component;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.ButtonGroup;
import javax.swing.DefaultCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JRadioButton;
import javax.swing.JTable;

import cs342.project1.mathNotebook.gui.RadioButtonPanel;

class RadioButtonEditor extends DefaultCellEditor implements ItemListener {
    RadioButtonPanel panel;

    public RadioButtonEditor(JCheckBox checkBox, RadioButtonPanel panel) {
      super(checkBox);
      this.panel = panel;
      ButtonGroup buttonGroup = new ButtonGroup();
      JRadioButton[] buttons = panel.getButtons();
      for (int i = 0; i < buttons.length; i++) {
    	buttons[i].setSelected(true);
        buttonGroup.add(buttons[i]);
        
        buttons[i].addItemListener(this);
      }
    }
    public Component getTableCellEditorComponent(JTable table,
            Object value, boolean isSelected, int row, int column) {
          if (value instanceof Integer) {
            panel.setSelectedIndex(((Integer) value).intValue());
          }
          return panel;
        }

        public Object getCellEditorValue() {
          return new Integer(panel.getSelectedIndex());
        }

        public void itemStateChanged(ItemEvent e) {
          super.setClickCountToStart(1);
          
        }
      }

